﻿using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Wage
{
    public abstract class WageRepositoryBase   //增加 查询 修改 删除 删除一个月  //回收站
    {

        public abstract Task<bool> AddAsync(SalaryItem salaryItem);
        public abstract Task<bool> AddAsync(List<SalaryItem> salaryItems);
        public abstract Task<IEnumerable<SalaryItem>> QueryByTimeAsync(DateTime startTime, DateTime endTime, bool Validation = true);
        public abstract Task<bool> UpdateAsync(SalaryItem salaryItem);
        public abstract Task<bool> FlipAmoutAsync(int Id);
        public abstract Task<bool> DeleteAsync(int Id);
        public abstract Task<bool> DeleteAsync(DateTime time);


    }
    public class WageRepository : WageRepositoryBase
    {
        /// <summary>
        /// 添加一个项
        /// </summary>
        /// <param name="salaryItem">添加的新项</param>
        /// <returns>是否成功添加</returns>
        public override async Task<bool> AddAsync(SalaryItem salaryItem)
        {
            using (WageContext context = new WageContext())
            {
                await context.SalaryItems.AddAsync(salaryItem);
                return await context.SaveChangesAsync() > 0;
            }

        }
        /// <summary>
        /// 添加多个数据
        /// </summary>
        /// <param name="salaryItems">数据列表</param>
        /// <returns>是否成功添加</returns>
        public override async Task<bool> AddAsync(List<SalaryItem> salaryItems)
        {
            using (WageContext context = new WageContext())
            {
                await context.AddRangeAsync(salaryItems);
                return await context.SaveChangesAsync() == salaryItems.Count;
            }

        }
        /// <summary>
        /// 删除一个数据
        /// </summary>
        /// <param name="Id">数据的ID</param>
        /// <returns>是否删除成功</returns>
        public override async Task<bool> DeleteAsync(int Id)
        {
            using (WageContext context = new WageContext())
            {
                var item = context.SalaryItems.Single(s => s.SalaryItemId == Id);
                item.Validation = false;
                return await context.SaveChangesAsync() > 0;
            }

        }
        /// <summary>
        /// 删除一个月的数据
        /// </summary>
        /// <param name="time">月份</param>
        /// <returns>是否删除成功</returns>
        public override async Task<bool> DeleteAsync(DateTime time)
        {
            using (WageContext context = new WageContext())
            {
                var items = context.SalaryItems.Where(s => s.Validation == true && s.Time == time);
                foreach (var salary in items)
                {
                    salary.Validation = false;
                }
                return await context.SaveChangesAsync() > 0;
            }

        }
        /// <summary>
        /// 修改一个项的数据
        /// </summary>
        /// <param name="Id">ID</param>
        /// <param name="salaryItem">新修改的项目</param>
        /// <returns>是否修改成功</returns>
        public override async Task<bool> UpdateAsync(SalaryItem salaryItem)
        {
            using (WageContext context = new WageContext())
            {
                var item = context.SalaryItems.Single(s => s.SalaryItemId == salaryItem.SalaryItemId);
                item.Name = salaryItem.Name;
                item.Time = salaryItem.Time;
                return await context.SaveChangesAsync() > 0;
            }

        }
        /// <summary>
        /// 按月查询数据
        /// </summary>
        /// <param name="startTime">开始时间</param>
        /// <param name="endTime">结束时间</param>
        /// <param name="Validation">是否是删除的</param>
        /// <returns>结果列表</returns>
        public override async Task<IEnumerable<SalaryItem>> QueryByTimeAsync(DateTime startTime, DateTime endTime, bool Validation = true)
        {
            using (WageContext context = new WageContext())
            {
                var items = context.SalaryItems.Where(s => s.Validation == Validation && s.Time >= startTime && s.Time <= endTime);

                return await items.ToListAsync();
            }

        }
        /// <summary>
        /// 修改金额为支出
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public override async Task<bool> FlipAmoutAsync(int Id)
        {
            using (WageContext context = new WageContext())
            {
                var item = context.SalaryItems.Single(s => s.SalaryItemId == Id);
                item.Amount = -item.Amount;
                return await context.SaveChangesAsync() > 0;
            }
        }
    }
}
